/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rooster.dbmanager.repository;

import org.rooster.dbmanager.datasource.DataSource;
import org.rooster.dbmanager.datasource.MySQLDataSource;

import java.io.*;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Renukshan
 */
public class RoosterDataSourceFactory {

    private static String dbType;
    private static DataSource roosterDataSource;

    /**
     * 
     * @return DataSource
     */
    public DataSource getDataSource() {

        Properties properties = new Properties();
        InputStream inputStream = RoosterDataSourceFactory.class.getResourceAsStream("/database.properties");
        try {
            properties.load(inputStream);
            dbType = properties.getProperty("db.type");

            //TODO: Idealy this logic should not be hardcoded.
            //This should be stored in a XML file and class should be decided based on that.

            if ("mysql".equals(dbType)) {
                roosterDataSource = new MySQLDataSource();
            }

        } catch (IOException e) {
            Logger.getLogger(RoosterDataSourceFactory.class.getName()).log(Level.SEVERE,
                    "Error occured while loading application properties file", e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException ignored) {
            }
        }

        return roosterDataSource;
    }
}
